package com.silky.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 线程安全List
 */
public class ThreadSafeList {
    List<Map<String, Object>> list;

    public void init(List<Map<String, Object>> list){
        synchronized (this){
            this.list = list;
        }
    }
    public Map<String, Object> remove(){
        synchronized (this){
            if(list!=null && !list.isEmpty()) {
                return list.remove(0);
            }else{
                return null;
            }
        }
    }
    public List<Map<String, Object>> removeByNum(int num){
        List<Map<String, Object>> retList  = new ArrayList<>();
        synchronized (this){
            while(retList.size()<num && list!=null && !list.isEmpty()) {
                retList.add(list.remove(0));
            }
            return retList;
        }
    }
    public int size(){
        synchronized (this){
            if(list != null) {
                return list.size();
            }else{
                return 0;
            }
        }
    }
}
